<?php

class FutureDm extends AppModel {

	/**
	 * Given a DM id, this function attempts to delete the given DM. If the delete is successful, it returns 0.
	 * @param $id the supplied DM id.
	 * @return int returns 0 incase of successful delete and 1 incase of insuccessful delete.
	 * @author Kareem Yousry
	 */ 
	public function remove($id) {

		if($this->delete($id)) { # DM was successfully deleted.
			return 0;
		}

		else { #The Supplied DM does not exist.
			return 1;
		}
	}

	/**
	 * Given the id of a DM, if this DM exists, the function checks if it is approved or not. If it is approved, 
	 * the function disproves the tweet and returns 0, else the function approves the tweet in the db and returns 1.
	 * If the supplied DM does not exist, the function returns 2.
	 * @param $id Function id.
	 * @return int. 0 if approved successfully, 1 if already approved and 2 if no such DM exists.
	 * @author Kareem Yousry
	 */
	public function approve($id) {
		$result = $this->find('all', array('conditions' => array('id' => $id), 'fields' => array('approved')));

		if($result) {

			if($result['0']['FutureDm']['approved'] == '0'){ # The specified DM has not yet been approved.
				$this->id = $id;
				$this->saveField('approved',true);
				return 0;

			} else { # Disprove DM
				$this->id = $id;
				$this->saveField('approved',false);
				return 1;
			}

		} else { # The specified DM does not exist.
			return 2;
		}
	}

	/**
	 * This function retrieves the FutureDms belonging to an account.
	 * @param int $accID The Supplied DM id.
	 * @return array of records present in the database.
	 * @author Kareem Yousry
	 */
	public function retrieveDMs($accID){
		return $this->find('all', array('conditions' => array('FutureDm.accID' => $accID)));
	}
	/**
	 * this function deletes a specified direct message from the database, if found
	 * @param int $dmID the supplied DM id.
	 * @return boolean , 1 incase of successful delete and 0 incase of insuccessful delete.
	 * @author "Mark Milad"
	 */ 
	public function JEdeleteDm($dmID){
		return $this->delete($dmID);
	}
	public function displayDms($userID, $accID){
		
			return $this->find('all', array('conditions' => array("FutureDm.userID" => $userID, "FutureDm.accID" => $accID)));

	}
}
